package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.IAlarmDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.*;
import cn.edu.seu.ssny.service.IAlarmService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
public class AlarmServiceImpl extends BaseServiceImpl<Alarm, Integer> implements IAlarmService {

    @Autowired
    private IAlarmDao alarmDao;

    @Override
    public IBaseDao<Alarm, Integer> getBaseDao() {
        return this.alarmDao;
    }

    @Override
    public List<Alarm> findByProject(Project project) {
        return alarmDao.findByProject(project);
    }

    @Override
    public void address(Alarm alarm) {
        Alarm dbAlarm = find(alarm.getId());
        dbAlarm.setAddressTime(new Date());
        dbAlarm.setResult(alarm.getResult());
        dbAlarm.setUserName(alarm.getUserName());
        dbAlarm.setStatus(true);
        update(dbAlarm);
    }


    @Override
    public void updateCreateTime(Alarm alarm) {
        Alarm dbAlarm = find(alarm.getId());
        dbAlarm.setCreateTime(alarm.getCreateTime());
        update(dbAlarm);
    }

    @Override
    public void create(Alarm alarm) {
        alarm.setStatus(false);
        save(alarm);
    }

    @Override
    public Optional<Alarm> findFirstByDeviceOrderByCreateTimeDesc(Device device) {
        return alarmDao.findFirstByDeviceOrderByCreateTimeDesc(device);
    }

    @Override
    public Integer countAlarmsByProjectAndStatus(Project project, Boolean status) {
        return alarmDao.countAlarmsByProjectAndStatus(project, status);
    }
}